﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PH.Model.Schools;
namespace PH.Data.Schools
{
    public class TeacherCommand
    {
        public static void SaveCheck(TeacherModel model, DBCentre db)
        {
            if (string.IsNullOrEmpty(model.RealName))
                throw new Utils.PhException("请提供教师姓名");
            if (model.SchoolID==0)
                throw new Utils.PhException("学校编号不存在");

            if (db != null)
            {
                if (db.DataContext.Teachers.Any(p => p.SchoolID == model.SchoolID && p.RealName == model.RealName && p.Guid != model.Guid))
                    throw new Utils.PhException("教师姓名重复");
                if (!db.DataContext.Schools.Any(p=>p.ID==model.SchoolID))
                    throw new Utils.PhException("无效的学校编号");
            }
        }

        public static TeacherModel CreateNewTeacher(TeacherModel model)
        {
            if (model != null)
            {
                model.CreatedOn = DateTime.Now;
                model.Guid = Guid.NewGuid();
                
                using (DBCentre db = new DBCentre())
                {
                    SaveCheck(model, db);
                    var dTeacher = Convertor.ConvertFrom<Teacher, TeacherModel>(model);
                    db.DataContext.Teachers.Add(dTeacher);
                    db.DataContext.SaveChanges();

                    model.ID = dTeacher.ID;
                    return model;
                }
            }
            return null;
        }

        public static void UpdateTeacher(TeacherModel model)
        {
            if (model != null && model.Guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dTeacher = db.DataContext.Teachers.FirstOrDefault(p => p.Guid == model.Guid);
                    if (dTeacher != null)
                    {
                        SaveCheck(model, db);
                        Convertor.UpdateValue(model, dTeacher);
                        db.DataContext.SaveChanges();
                    }
                    else
                        throw new Utils.PhException("不存在的教师");
                }
            }
        }

        public static TeacherModel GetTeacher(Guid guid)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    var dTeacher = db.DataContext.Teachers.FirstOrDefault(p => p.Guid == guid);
                    if (dTeacher != null)
                    {
                        var model = Convertor.ConvertTo<TeacherModel, Teacher>(dTeacher);
                        return model;
                    }
                }
            }
            return null;
        }

        public static TeacherCollection GetStudents(TeacherQuery sea)
        {
            var result = new TeacherCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Teacher> q = db.DataContext.Teachers;
                if (sea != null)
                {
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.RealName==sea.Key || p.Mobile == sea.Key);
                    if (sea.SchoolGuid != Guid.Empty)
                        q = q.Where(p => p.School.Guid == sea.SchoolGuid);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<TeacherModel, Teacher, TeacherCollection>(lst);
                    result.TotalRecordsCount = n;
                }
            }
            return result;
        }

        public static void DeleteTeacher(Guid guid)
        {
            if (guid != Guid.Empty)
            {
                using (DBCentre db = new DBCentre())
                {
                    db.Delete<Teacher>(p => p.Guid == guid);
                    db.DataContext.SaveChanges();
                }
            }
        }
    }
}
